package com.xiaoxin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxin.common.encrypt.properties.ApiDecryptProperties;
import com.xiaoxin.common.encrypt.utils.EncryptUtils;
import com.xiaoxin.domain.UserSchema;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.xiaoxin.common.encrypt.annotation.ApiEncrypt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

@Slf4j
@Tag(name = "testEncrypt")
@RestController
@RequestMapping("/Encrypt")
public class Controller {
    @Autowired
    private  ApiDecryptProperties properties;
    @ApiEncrypt
    @Operation(summary = "创建用户-Schema",description = "根据姓名创建用户1")
    @PostMapping("/createOneSchema")
    public ResponseEntity<UserSchema> createOneSchema(@RequestBody UserSchema user){
        return ResponseEntity.ok(user);
    }

    @Operation(summary = "restTemplateCreateOneSchema创建用户-Schema",description = "根据姓名创建用户1")
    @PostMapping("/restTemplateCreateOneSchema")
    public ResponseEntity<UserSchema> restTemplateCreateOneSchema(@RequestBody UserSchema user){
        RestTemplate restTemplate = restTemplate();
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String aesPassword="123456123456123456123456";
        String aesPasswordBase64 = EncryptUtils.encryptByBase64(aesPassword);
        String aesPasswordPublicEncrypt = EncryptUtils.encryptByRsa(aesPasswordBase64, properties.getPublicKey());
        System.out.println(aesPasswordBase64);
        System.out.println(aesPasswordPublicEncrypt);
        //headers.set("encrypt-key", aesPasswordPublicEncrypt);
        headers.set("encrypt-key", "111");

        String requestBody = EncryptUtils.encryptByAes(JSONObject.toJSONString(user), aesPassword);
        // 创建HttpEntity，包装请求头和请求体
        HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
        // 发起POST请求
        ResponseEntity<UserSchema> response = restTemplate.postForEntity("http://localhost:8089/Encrypt/createOneSchema", entity, UserSchema.class);
        UserSchema body =response.getBody();
        System.out.println(response);
        System.out.println(body);
        return ResponseEntity.ok(body);
    }


    public RestTemplate restTemplate(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        // 设置读取超时
        factory.setReadTimeout(500000);
        // 设置连接超时，单位毫秒
        factory.setConnectTimeout(500000);
        RestTemplate restTemplate = new RestTemplate(factory);
        // 支持中文编码
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        log.info("RestTemplate初始化完成");
        return restTemplate;
    }




}
